package cj.web.marriage.controllers;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.constants.MemuEnum;
import cj.web.admin.dao.ConfigInfoDAO;
import cj.web.admin.domain.ConfigInfo;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.dao.GameDAO;
import cj.web.marriage.domain.Game;

@Path("/game")
public class GameController extends BaseController{
	
	private static final String CODE = "JF_TYPE";

	private static Logger logger = Logger.getLogger(GameController.class.getName());
	
	@Autowired
	private GameDAO gameDao;
	
	@Autowired
	private ConfigInfoDAO configInfoDao;
	
	@Get("show")
	public String show(Model model,Game game) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagejfgc);
        
        List<ConfigInfo> configInfolis = configInfoDao.queryByCode(CODE);
        
        model.add("configInfolis",configInfolis);
        
		return "/views/marriage/gameList.vm";
	}

	@Post("getList")
	public String queryList(Game game) {
	
		JSONObject jo = new JSONObject();
		
		List<ConfigInfo> configInfolis = configInfoDao.queryByCode(CODE);
		Map rep = new HashMap();
		for(ConfigInfo obj:configInfolis){
			rep.put(obj.getValue(), obj.getName());
		}
		
		List<Game> objs = gameDao.query(game);
		
		for(Game obj:objs){
			if(obj.getType()==null){
				obj.setTypeName("");
			}else{
				obj.setTypeName(rep.get(obj.getType().toString()).toString());
			}
			
		}
		
		int size = gameDao.querySize(game);
		jo.put("data", objs);
		jo.put("draw", game.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagejfgc);
        List<ConfigInfo> configInfolis = configInfoDao.queryByCode(CODE);
        model.add("configInfolis",configInfolis);
		return "/views/marriage/gameDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Game game,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			game.setCreateUser(returnSessionObj(request).getAccount());
			game.setUpdateUser(returnSessionObj(request).getAccount());
			gameDao.create(game);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id) {
		Game obj = gameDao.queryById(id);
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagejfgc);
        List<ConfigInfo> configInfolis = configInfoDao.queryByCode(CODE);
        model.add("configInfolis",configInfolis);
		return "/views/marriage/gameEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Game game,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			game.setUpdateUser(returnSessionObj(request).getAccount());
			gameDao.update(game);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id) {
		gameDao.deleteById(id);
		return "r:show";
	}
	
	@Post("doUpload")
    public String doUpload(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (file != null && !file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath_Marriage");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
                ImageUtils.scale2(path+"/"+fileName, path+"/s_"+fileName, 120, 120, true);//缩率图
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
}
